Filter results inside a polygon
Use Algolia’s geo search capabilities to filter results inside a polygon.
In this tutorial, you’ll learn how to filter results around a polygonal location. This location can either be set manually or taken from the current user position.
Dataset
The dataset contains 3000+ of the biggest airports in the world.
To tell Algolia where each record is located,
we need to have the latitude and longitude stored in the _geoloc
attribute.
You can download the dataset here. Have look at how to import it in Algolia here
Initialize the client
Configure Index Settings
Even if we want to sort by distance to a location, we need the textual relevance to be good in case we refine the search with a query.
For that let’s configure the index.
Searchable attributes
We’re going to search in our 4 textual attributes: name
, city
, country
and iata_code
.
Custom Ranking
We will use the number of other connected airports to any airport as a ranking metric - the more connection the better.
Filtering inside a polygonal area
Let’s filter inside the United State of America.
USA can be considered as a polygon:
To filter inside this polygon we need the latitude and longitude of all the points:
- 42.01,-124.31,
- 48.835509470063045,-124.40453125000005
- 45.01082951668149,-65.95726562500005
- 31.247243545293433,-81.06578125000004
- 25.924152577235226,-97.68234374999997
- 32.300311895879545,-117.54828125
We are going to use the insidePolygon
parameter:
We are using the empty query (''
) because we want all airports.